package com.web.servlet;

import java.io.IOException;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * My Servlet
 * @ClassName: MyServlet
 * @Description: 浏览器上输入自定义的servlet映射路径，
 * 证明传统的servlet的应用也是可以放到springboot中来操作的
 * @author liuwj3
 * @date 2019年2月14日 下午8:48:01
 *
 */
// @WebServlet默认是同步的，所以要开启异步
@WebServlet(urlPatterns = "/my/servlet", asyncSupported = true)
public class MyServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 改造成异步方式
		AsyncContext asyncContext = req.startAsync();
		// 使用lambda表达式
		asyncContext.start(() -> {
			try {
				resp.getWriter().println("hello world to my servlet!");
				// 触发完成，不然会卡死
				asyncContext.complete();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});
		
	}
	
}
